package com.gs.api.projects.shejiao.web;

import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayMicropayResult;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.gs.api.config.api.ApiConfig;
import com.gs.api.core.baen.pay.PayInfoDto;
import com.gs.api.core.entity.TPaymentTransaction;
import com.gs.api.core.service.ITPaymentTransactionService;
import com.gs.api.projects.shejiao.service.PayService;
import com.gs.api.util.Arith;
import com.gs.api.util.IPHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author guoyunfei on 2021/10/9 17:12
 * @className PayController
 * @description TODO
 * @Version 1.0
 */

@Api(tags = "微信支付")
@RestController
@RequestMapping("{appid}")
public class PayController {

    @Autowired
    private ApiConfig apiConfig;

    @Autowired
    private PayService payService;

    @Autowired
    private WxPayService wxMiniPayService;

    @Autowired
    private ITPaymentTransactionService itPaymentTransactionService;

    @ApiOperation(value = "微信支付", notes="微信支付")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
        @ApiImplicitParam(name = "openid", value = "openid", required = true, dataType = "String"),
    })
    @GetMapping("/pay")
    public ResponseEntity<WxPayMpOrderResult> pay(@PathVariable String appid, String openid,String type) throws WxPayException {

        PayInfoDto payInfo = payService.pay(type,appid);
        WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
        orderRequest.setBody(payInfo.getBody());
        orderRequest.setOutTradeNo(payInfo.getPayNo());

        orderRequest.setTotalFee((int) Arith.mul(payInfo.getPayAmount(), 100));
        orderRequest.setSpbillCreateIp(IPHelper.getIpAddr());
        orderRequest.setNotifyUrl(apiConfig.getDomainName() + apiConfig.getContextPath() + "/notice/pay/order");
        orderRequest.setTradeType(WxPayConstants.TradeType.JSAPI);
        orderRequest.setOpenid(openid);
        WxPayMpOrderResult order = wxMiniPayService.createOrder(orderRequest);

        save(payInfo,openid,type,appid);

        return ResponseEntity.ok(order);
    }

    /**
     * 新增支付记录
     * @param payInfo
     * @param openid
     * @param type
     * @return
     */
    public boolean save(PayInfoDto payInfo,String openid,String type,String appid){
        //增加付款交易信息
        TPaymentTransaction tPaymentTransaction = new TPaymentTransaction();
        tPaymentTransaction.setAppid(appid);
        tPaymentTransaction.setPayType(1);
        tPaymentTransaction.setConsumeType(Integer.valueOf(type));
        tPaymentTransaction.setPayAmount(payInfo.getPayAmount().toString());
        tPaymentTransaction.setPayStatus(0);
        tPaymentTransaction.setPayNumber(payInfo.getPayNo());
        tPaymentTransaction.setUserId(openid);
        tPaymentTransaction.setOrderId(payInfo.getPayNo());
        if("1".equals(type) || "2".equals(type)){
            tPaymentTransaction.setCommodityName("1".equals(type) ?"盲盒纸条存入" :"盲盒纸条取出");
        }else if("3".equals(type)){
            tPaymentTransaction.setCommodityName("成为推广会员用户");
        }

        tPaymentTransaction.setCreateTime(new Date());
        return itPaymentTransactionService.save(tPaymentTransaction);
    }

}
